home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Programming / OUI / envman.lha / EnvManager / envwin.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-11  |  12.1 KB  |  369 lines

  1. // $Id$
  2. #include <exec/types.h>
  3. #include <dos/dosextens.h>
  4. #include <dos/var.h>
  5. #include <libraries/gadtools.h>
  6. #include <proto/dos.h>
  7. #include <proto/amigaguide.h>
  8. #include <mydebug.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11.  
  12. #define NO_CLASS_WINDOW_INLINES
  13. #include <screen.h>
  14. #include <gadgets/fbutton.h>
  15. #include <gadgets/cstring.h>
  16. #include <gadgets/cnumber.h>
  17. #include <gadgets/listview.h>
  18. #include <gadgets/checkbox.h>
  19. #include <locale.h>
  20. #include <compiler.h>
  21.  
  22. #include "envdef.h"
  23. #include "envwin.h"
  24. #include "enventry.h"
  25. #include "envopt.h"
  26.  
  27. IMPORT catalog *ecat ;
  28.  
  29. GLOBAL const lstring FAR local NPARMS(ecat, "L_ocal", "MSG_LOCAL_GAD", 0) ;
  30. GLOBAL const lstring FAR global LPARMS(ecat, "G_lobal", "MSG_GLOBAL_GAD") ;
  31. GLOBAL const lstring FAR archive LPARMS(ecat, "A_rchive", "MSG_ARCHIVE_GAD") ;
  32. GLOBAL const lstring FAR locsize LPARMS(ecat, "Local Size", "MSG_LOCAL_SIZE") ;
  33. GLOBAL const lstring FAR globsize LPARMS(ecat, "Global Size", "MSG_GLOBAL_SIZE") ;
  34. GLOBAL const lstring FAR newentry LPARMS(ecat, "Create/Search", "MSG_CREATE_AND_SEARCH") ;
  35. GLOBAL const lstring FAR process LPARMS(ecat, "Process", "MSG_PROCESS") ;
  36. GLOBAL const lstring FAR help LPARMS(ecat, "_Help", "MSG_HELP_GAD") ;
  37.  
  38. GLOBAL const lstring FAR newvar NPARMS(ecat, "Var Name:", "MSG_VAR_NAME", 50) ;
  39. GLOBAL const lstring FAR unnamed LPARMS(ecat, "UnNamed", "MSG_UNNAMED") ;
  40. GLOBAL const lstring FAR nothing LPARMS(ecat, "Nothing", "MSG_NOTHING") ;
  41.  
  42. GLOBAL const lstring FAR alias NPARMS(ecat, "Alias", "MSG_ALIAS", 100) ;
  43. GLOBAL const lstring FAR local2 LPARMS(ecat, "Local", "MSG_LOCAL") ;
  44. GLOBAL const lstring FAR global2 LPARMS(ecat, "Global", "MSG_GLOBAL") ;
  45. GLOBAL const lstring FAR archived LPARMS(ecat, "Archived", "MSG_ARCHIVED") ;
  46. GLOBAL const lstring FAR binary LPARMS(ecat, "Binary", "MSG_BINARY") ;
  47. GLOBAL const lstring FAR nullterm LPARMS(ecat, "Null Term", "MSG_NULL_TERM") ;
  48. GLOBAL const lstring FAR vsize LPARMS(ecat, "Size", "MSG_SIZE") ;
  49. GLOBAL const lstring FAR contents LPARMS(ecat, "_Contents", "MSG_CONTENTS_GAD") ;
  50. GLOBAL const lstring FAR contents2 LPARMS(ecat, "Contents", "MSG_CONTENTS") ;
  51. GLOBAL const lstring FAR name LPARMS(ecat, "_Name", "MSG_NAME_GAD") ;
  52. GLOBAL const lstring FAR edit LPARMS(ecat, "_Edit", "MSG_EDIT_GAD") ;
  53. GLOBAL const lstring FAR import LPARMS(ecat, "_Import", "MSG_IMPORT_GAD") ;
  54. GLOBAL const lstring FAR export LPARMS(ecat, "_Export", "MSG_EXPORT_GAD") ;
  55. GLOBAL const lstring FAR edition LPARMS(ecat, "Edition", "MSG_EDITION") ;
  56.  
  57. GLOBAL const lstring FAR importfrom NPARMS(ecat, "Import from:", "MSG_IMPORT_TITLE", 200) ;
  58. GLOBAL const lstring FAR exportto LPARMS(ecat, "Export to:", "MSG_EXPORT_TITLE") ;
  59.  
  60. STRPTR StringRequest(STRPTR title, STRPTR def) ;
  61.  
  62. IMPORT nlist                *envloclist ;
  63. IMPORT nlist                *envlist ;
  64. IMPORT nlist                *envarclist ;
  65. IMPORT TTextAttr            Tiny ;
  66. IMPORT TTextAttr            Normal ;
  67. IMPORT LONG                 envsize ;
  68. IMPORT LONG                 envlocsize ;
  69. IMPORT Process              *proc ;
  70. IMPORT AMIGAGUIDECONTEXT    HelpHandle ;
  71.  
  72. nlist                       *curlist ;
  73. enventry                    *curenv ;
  74.  
  75. WORD winZoom[4] = { 10, 10, 200, 11 };
  76.  
  77. void envwindow::updateenv(enventry *curenv,
  78.         BOOL wasarch, BOOL wasglob,
  79.         BOOL isarch, BOOL isglob,
  80.         BOOL fromString)
  81. {
  82. char    path[256] ;
  83. LONG flags ;
  84. enventry *genv ;
  85. enventry *aenv ;
  86.  
  87.     if (wasglob) envsize -= oldsize ;
  88.  
  89.     if (isarch) {
  90.         flags = GVF_GLOBAL_ONLY|GVF_SAVE_VAR ;
  91.         if (curenv->isbinary() && !fromString)
  92.             flags |= GVF_BINARY_VAR ;
  93.         if (!curenv->isnullterm())
  94.             flags |= GVF_DONT_NULL_TERM ;
  95.         if (!wasarch) {
  96.             envarclist->enqueue(new enventry(curenv->name,
  97.                 curenv->contents,
  98.                 curenv->size,
  99.                 ARCHIVED_TYPE)) ;
  100.         }
  101.         SetVar(curenv->name, curenv->contents, curenv->size, flags) ;
  102.     }
  103.     if (isglob) {
  104.         envsize += curenv->size ;
  105.         flags = GVF_GLOBAL_ONLY ;
  106.         if (curenv->isbinary() && !fromString)
  107.             flags |= GVF_BINARY_VAR ;
  108.         if (!curenv->isnullterm())
  109.             flags |= GVF_DONT_NULL_TERM ;
  110.         if (!wasglob) {
  111.             envlist->enqueue(new enventry(curenv->name,
  112.                 curenv->contents,
  113.                 curenv->size,
  114.                 GLOBAL_TYPE)) ;
  115.         }
  116.         SetVar(curenv->name, curenv->contents, curenv->size, flags) ;
  117.     }
  118.     genv = findname(envlist, curenv->name) ;
  119.     aenv = findname(envarclist, curenv->name) ;
  120.     if (wasarch && !isarch) {
  121.         envarclist->remove(aenv) ;
  122.         strcpy(path, "ENVARC:") ;
  123.         AddPart(path, aenv->name, 256) ;
  124.         DeleteFile(path) ;
  125.         delete aenv ;
  126.     }
  127.     if (wasglob && !isglob) {
  128.         envlist->remove(genv) ;
  129.         strcpy(path, "ENV:") ;
  130.         AddPart(path, genv->name, 256) ;
  131.         DeleteFile(path) ;
  132.         delete genv ;
  133.     }
  134.     lenv->reset(curlist, lenv->curtop, lenv->cursel) ;
  135.     es->set(envsize) ;
  136. }
  137.  
  138. void envwindow::floclist(gadget *g, ULONG classe, USHORT code)
  139. {
  140. BOOL wasglob ;
  141. BOOL wasarch ;
  142. optwindow *ow ;
  143.  
  144.     curenv = (enventry *)envloclist->get(code) ;
  145.     if (curenv) {
  146.         wasglob = curenv->isglobal() ;
  147.         wasarch = curenv->isarchived() ;
  148.         ow = new optwindow(10, 10, 400, 120) ;
  149.         if (wasglob) oldsize = curenv->size ;
  150.         ow->open(ws) ;
  151.         ow->eventloop() ;
  152.         if (ow->okflag) {
  153.             updateenv(curenv, wasarch, wasglob,
  154.                     ow->carch->cursel, ow->cglob->cursel, FALSE) ;
  155.         }
  156.         ow->close() ;
  157.         delete ow ;
  158.     }
  159. }
  160. void envwindow::flist(gadget *g, ULONG classe, USHORT code)
  161. {
  162.     if (curlist == envlist)
  163.         globlist(code) ;
  164.     else
  165.         arclist(code) ;
  166. }
  167.  
  168. void envwindow::globlist(USHORT code)
  169. {
  170. optwindow *ow ;
  171. BOOL wasglob ;
  172. BOOL wasarch ;
  173. enventry    *genv ;
  174. BOOL        fromString = FALSE ;
  175.  
  176.     curenv = (enventry *)envlist->get(code) ;
  177.     if (curenv) {
  178.         wasglob = curenv->isglobal() ;
  179.         wasarch = curenv->isarchived() ;
  180.  
  181.         ow = new optwindow(10, 10, 400, 120) ;
  182.         ow->open(ws) ;
  183.         ow->eventloop() ;
  184.         if (ow->okflag) {
  185.             if (ow->scontents) {
  186.                 fromString = TRUE ;
  187.                 delete curenv->contents ;
  188.                 if (wasglob) oldsize = curenv->size ;
  189.                 curenv->size = strlen(ow->scontents->curstring) ;
  190.                 curenv->contents = new char[curenv->size+1] ;
  191.                 strcpy(curenv->contents, ow->scontents->curstring) ;
  192.                 curenv->setentry() ;
  193.                 if (curenv->isarchived()) {
  194.                     genv = findname(envarclist, curenv->name) ;
  195.                     genv->size = curenv->size ;
  196.                     delete genv->contents ;
  197.                     genv->contents = new char[genv->size+1] ;
  198.                     strcpy(genv->contents, curenv->contents);
  199.                     genv->setentry() ;
  200.                 }
  201.             }
  202.             updateenv(curenv, wasarch, wasglob, ow->carch->cursel, ow->cglob->cursel, fromString) ;
  203.         }
  204.         ow->close() ;
  205.         delete ow ;
  206.     }
  207. }
  208.  
  209.  
  210. void envwindow::arclist(USHORT code)
  211. {
  212. optwindow *ow ;
  213. BOOL wasglob ;
  214. BOOL wasarch ;
  215. enventry    *genv ;
  216. BOOL        fromString = FALSE ;
  217.  
  218.     curenv = (enventry *)envarclist->get(code) ;
  219.     if (curenv) {
  220.         wasglob = curenv->isglobal() ;
  221.         wasarch = curenv->isarchived() ;
  222.  
  223.         ow = new optwindow(10, 10, 400, 120) ;
  224.         ow->open(ws) ;
  225.         ow->eventloop() ;
  226.         if (ow->okflag) {
  227.             if (ow->scontents) {
  228.                 fromString = TRUE ;
  229.                 if (wasglob) oldsize = curenv->size ;
  230.                 curenv->size = strlen(ow->scontents->curstring) ;
  231.                 curenv->contents = new char[curenv->size+1] ;
  232.                 strcpy(curenv->contents, ow->scontents->curstring) ;
  233.                 curenv->setentry() ;
  234.                 if (curenv->isglobal()) {
  235.                     genv = findname(envlist, curenv->name) ;
  236.                     genv->size = curenv->size ;
  237.                     delete genv->contents ;
  238.                     genv->contents = new char[genv->size+1] ;
  239.                     strcpy(genv->contents, curenv->contents);
  240.                     genv->setentry() ;
  241.                 }
  242.             }
  243.             updateenv(curenv, wasarch, wasglob, ow->carch->cursel, ow->cglob->cursel, fromString) ;
  244.         }
  245.         ow->close() ;
  246.         delete ow ;
  247.     }
  248. }
  249.  
  250. void envwindow::fswap(gadget *g, ULONG classe, USHORT code)
  251. {
  252.     curlist = (code) ? envarclist : envlist ;
  253.     lenv->reset(curlist, lenv->curtop, lenv->cursel) ;
  254. }
  255.  
  256. void envwindow::fnew(gadget *g, ULONG classe, USHORT code)
  257. {
  258. STRPTR name ;
  259. enventry *ev ;
  260.  
  261.     name = StringRequest(newvar.charptr(), "") ;
  262.     if (name ) {
  263.         if (!strlen(name))
  264.             ev = NULL ;
  265.         else if (!(ev = findvar(name))) {
  266.              if (ev = new enventry(name, nothing.charptr(),
  267.                 strlen(nothing), GLOBAL_TYPE))
  268.              envlist->enqueue(ev) ;
  269.         }
  270.         delete name ;
  271.         if (ev)
  272.             if (ev->islocale() || ev->isalias())
  273.                 floclist(NULL, NULL, ev->indexof()) ;
  274.             else if (ev->isglobal())
  275.                 globlist(ev->indexof()) ;
  276.             else
  277.                 arclist(ev->indexof()) ;
  278.     }
  279. }
  280.  
  281. void envwindow::fpurge(gadget *g, ULONG classe, USHORT code)
  282. {
  283. }
  284.  
  285. void envwindow::fhelp(gadget *g, ULONG classe, USHORT code)
  286. {
  287.     if (AmigaGuideBase && HelpHandle)
  288.         SendAmigaGuideCmd(HelpHandle, "LINK MAIN", NULL) ;
  289. }
  290.  
  291.  
  292. void envwindow::handlevkey(USHORT code)
  293. {
  294.     g->parsegadgets(code) ;
  295. }
  296.  
  297. void envwindow::open(screen *ns)
  298. {
  299. long mw ;
  300. long nl ;
  301. static  char titre[80] ;
  302.  
  303.     ws = ns ;
  304.     sprintf(titre, "EnvManager Â© 1997, D. Lorre, %s: %s",
  305.         process.string, proc->pr_Task.tc_Node.ln_Name) ;
  306.  
  307.     mw = ws->scr->Width - 40 ;
  308.  
  309.     g = new gadgetlist(this, 10) ;
  310.  
  311.     // calcul du nombre de lignes par liste
  312.  
  313.     nl = (ws->scr->Height -                             // Hauteur maxi
  314.          (ws->winbarheight + ws->scr->WBorBottom +      // Bords de la fenĂȘtre
  315.          (Tiny.tta_YSize*3/2 + 8) * 3 +                 // cstrings
  316.          12 * 3 +                                       // listes
  317.          Normal.tta_YSize*3/2 +                         // ligne du bas
  318.          + 8)) /                                        // marge
  319.          (3*Tiny.tta_YSize) ;
  320.  
  321.     g->setfont(&Tiny) ;
  322.     g->box(20, ws->winbarheight+8, g->ltext(local)+8, g->fontheight*3/2) ;
  323.     new cstring(g, NULL, local, NULL, FALSE, PLACETEXT_IN|NG_HIGHLABEL) ;
  324.  
  325.     g->box(20, g->maxh+4, mw, g->fontheight*nl+4) ;
  326.     lloc = new listview(g, WFUNC(&envwindow::floclist), NULL, NULL, envloclist, 0, 0) ;
  327.  
  328.     curlist = envlist ;
  329.     g->box(20, g->maxh+8, (g->fontheight*3*ws->xratio)/(ws->yratio*2), g->fontheight*3/2) ;
  330.     new checkbox(g, WFUNC(&envwindow::fswap), archive, FALSE, PLACETEXT_RIGHT|NG_HIGHLABEL) ;
  331.  
  332.     g->box(20, g->maxh+4, mw, g->fontheight*nl*2+4) ;
  333.     lenv = new listview(g, WFUNC(&envwindow::flist), NULL, NULL, envlist, 0, 0) ;
  334.     g->setfont(&Normal) ;
  335.  
  336.     g->box(20+g->ltext(globsize)+8, g->maxh+8, g->ltext("999999")+8, g->fontheight*3/2) ;
  337.     es = new cnumber(g, NULL, globsize, envsize, TRUE, PLACETEXT_LEFT|NG_HIGHLABEL) ;
  338.  
  339.     g->box(g->left+g->width+g->ltext(locsize)+16, g->top, g->ltext("999999")+8, g->fontheight*3/2) ;
  340.     new cnumber(g, NULL, locsize, envlocsize, TRUE, PLACETEXT_LEFT|NG_HIGHLABEL) ;
  341.  
  342.     g->box(g->left+g->width+8, g->top, g->ltext(newentry)+8, g->fontheight*3/2) ;
  343.     new fbutton(g, WFUNC(&envwindow::fnew), newentry, FALSE) ;
  344.  
  345.     if (AmigaGuideBase && HelpHandle) {
  346.         g->box(g->left+g->width+8, g->top, g->ltext(help)+8, g->fontheight*3/2) ;
  347.         new fbutton(g, WFUNC(&envwindow::fhelp), help, FALSE) ;
  348.     }
  349.  
  350.     width = short(g->maxw-ws->scr->WBorLeft+20) ;
  351.     height = short(g->maxh-ws->winbarheight+8) ;
  352.  
  353.     winZoom[2] = g->ltext(titre) + 120 ; // about 40 pixels per (3) sysim
  354.     winZoom[3] = ns->winbarheight ;
  355.  
  356.     _open(NULL, SCROLLERIDCMP|ARROWIDCMP|STRINGIDCMP|BUTTONIDCMP|
  357.                 IDCMP_VANILLAKEY|IDCMP_MOUSEBUTTONS|IDCMP_CLOSEWINDOW,
  358.         WA_Title,           titre,
  359.         WA_Gadgets,         g->glist,
  360.         WA_DragBar,         TRUE,
  361.         WA_DepthGadget,     TRUE,
  362.         WA_CloseGadget,     TRUE,
  363.         WA_Activate,        TRUE,
  364.         WA_Zoom,            winZoom,
  365.         TAG_DONE) ;
  366.  
  367.     if (initok) g->updategadgets() ;
  368. }
  369.